{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "mount_file_id": "1F5RqKTOUiZkJ8PmeFHSabZm8euh7cp5j",
      "authorship_tag": "ABX9TyM14vR+Yx9UxGNrmkIrQILK",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/chenyu313/Colaboratory_note/blob/main/NeuralKG.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "第一步：更改设置，设置代码执行程序为GPU\n",
        "\n",
        "第二步：连接云端主机\n",
        "\n",
        "第三步：装载Google硬盘选项，点击选择\n",
        "\n",
        "第四步：出现drive文件夹表示连接成功\n",
        "\n",
        "第五步：可以看到相关文件夹，如果需要访问的话，注意设置地址"
      ],
      "metadata": {
        "id": "g4Ohral28hOC"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TlkpOr2D7AZX",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "bf71c18b-7613-43b7-e637-cec9e98b0ef2"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/drive/MyDrive/NeuralKG-main\n"
          ]
        }
      ],
      "source": [
        "#找到当前程序文件夹\n",
        "%cd /content/drive/MyDrive/NeuralKG-main"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#安装 miniconda 改变 colab python 版本\n",
        "%%bash\n",
        "MINICONDA_INSTALLER_SCRIPT=Miniconda3-4.5.4-Linux-x86_64.sh\n",
        "MINICONDA_PREFIX=/usr/local\n",
        "wget https://repo.continuum.io/miniconda/$MINICONDA_INSTALLER_SCRIPT\n",
        "chmod +x $MINICONDA_INSTALLER_SCRIPT\n",
        "./$MINICONDA_INSTALLER_SCRIPT -b -f -p $MINICONDA_PREFIX\n",
        "\n",
        "conda install --channel defaults conda python=3.8 --yes\n",
        "conda update --channel defaults --all --yes"
      ],
      "metadata": {
        "id": "QPoQvePwABoA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#python 版本改变后的新安装包路径加入系统路径\n",
        "import sys\n",
        "sys.path.append(\"/usr/local/lib/python3.8/site-packages\")"
      ],
      "metadata": {
        "id": "smFfc9bTAFPH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install torch==1.9.1+cu111 -f https://download.pytorch.org/whl/torch_stable.html"
      ],
      "metadata": {
        "id": "SLzJbSS881-R"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install dgl-cu111 dglgo -f https://data.dgl.ai/wheels/repo.html"
      ],
      "metadata": {
        "id": "VFqUx_-O-CdB"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!python setup.py install"
      ],
      "metadata": {
        "id": "07Dbr_gS_APY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#查看是否有GPU\n",
        "\n",
        "import torch\n",
        "print(torch.cuda.is_available())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zfUZHQ_P-Uvo",
        "outputId": "871ecac2-931a-4995-aced-aef9edb24c8a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "True\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#补充漏的包\n",
        "!pip install pytorch-lightning"
      ],
      "metadata": {
        "id": "o2KgIB0oB69Y"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install IPython"
      ],
      "metadata": {
        "id": "jw4vBpXZCxbP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!python demo.py"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kIJJbAfq-b05",
        "outputId": "d972062e-cb17-4606-9c9c-57912c5bbff8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "This demo is powered by \u001b[1;32mNeuralKG \u001b[0m\n",
            "Global seed set to 321\n",
            "GPU available: True, used: True\n",
            "TPU available: False, using: 0 TPU cores\n",
            "IPU available: False, using: 0 IPUs\n",
            "LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]\n",
            "\n",
            "  | Name  | Type     | Params\n",
            "-----------------------------------\n",
            "0 | model | TransH   | 7.5 M \n",
            "1 | loss  | Adv_Loss | 7.5 M \n",
            "-----------------------------------\n",
            "7.5 M     Trainable params\n",
            "2         Non-trainable params\n",
            "7.5 M     Total params\n",
            "30.030    Total estimated model params size (MB)\n",
            "Validation sanity check: 0it [00:00, ?it/s]/usr/local/lib/python3.8/site-packages/torch/utils/data/dataloader.py:478: UserWarning: This DataLoader will create 16 worker processes in total. Our suggested max number of worker in current system is 2, which is smaller than what this DataLoader is going to create. Please be aware that excessive worker creation might get DataLoader running slow or even freeze, lower the worker number to avoid potential slowness/freeze if necessary.\n",
            "  warnings.warn(_create_warning_msg(\n",
            "Global seed set to 321\n",
            "Epoch 0:   0% 0/265 [00:00<?, ?it/s] /usr/local/lib/python3.8/site-packages/pytorch_lightning-1.5.10-py3.8.egg/pytorch_lightning/utilities/data.py:59: UserWarning: Trying to infer the `batch_size` from an ambiguous collection. The batch size we found is 10. To avoid any miscalculations, use `self.log(..., batch_size=batch_size)`.\n",
            "  warning_cache.warn(\n",
            "Epoch 49:  21% 280/1361 [00:31<01:59,  9.02it/s, loss=0.359, v_num=1]\n",
            "Validating: 0it [00:00, ?it/s]\u001b[A\n",
            "Validating:   0% 0/1096 [00:00<?, ?it/s]\u001b[A\n",
            "Epoch 49:  22% 300/1361 [00:33<01:58,  8.92it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  24% 320/1361 [00:34<01:52,  9.23it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  25% 340/1361 [00:35<01:47,  9.53it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  26% 360/1361 [00:36<01:41,  9.82it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  28% 380/1361 [00:37<01:37, 10.08it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  29% 400/1361 [00:38<01:32, 10.34it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  31% 420/1361 [00:39<01:28, 10.58it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  32% 440/1361 [00:40<01:25, 10.81it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  34% 460/1361 [00:41<01:21, 11.03it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  35% 480/1361 [00:42<01:18, 11.23it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  37% 500/1361 [00:43<01:15, 11.44it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  38% 520/1361 [00:44<01:12, 11.63it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  40% 540/1361 [00:45<01:09, 11.81it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  41% 560/1361 [00:46<01:06, 11.99it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  43% 580/1361 [00:47<01:04, 12.17it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  44% 600/1361 [00:48<01:01, 12.34it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  46% 620/1361 [00:49<00:59, 12.53it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  47% 640/1361 [00:50<00:56, 12.71it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  48% 660/1361 [00:51<00:54, 12.88it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  50% 680/1361 [00:52<00:52, 13.04it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  51% 700/1361 [00:53<00:50, 13.20it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  53% 720/1361 [00:53<00:47, 13.36it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  54% 740/1361 [00:54<00:45, 13.51it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  56% 760/1361 [00:55<00:44, 13.66it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  57% 780/1361 [00:56<00:42, 13.80it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  59% 800/1361 [00:57<00:40, 13.93it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  60% 820/1361 [00:58<00:38, 14.06it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  62% 840/1361 [00:59<00:36, 14.17it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  63% 860/1361 [01:00<00:35, 14.26it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  65% 880/1361 [01:01<00:33, 14.36it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  66% 900/1361 [01:02<00:31, 14.45it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  68% 920/1361 [01:03<00:30, 14.54it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  69% 940/1361 [01:04<00:28, 14.62it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  71% 960/1361 [01:05<00:27, 14.71it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  72% 980/1361 [01:06<00:25, 14.79it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  73% 1000/1361 [01:07<00:24, 14.87it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  75% 1020/1361 [01:08<00:22, 14.95it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  76% 1040/1361 [01:09<00:21, 15.03it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  78% 1060/1361 [01:10<00:19, 15.11it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  79% 1080/1361 [01:11<00:18, 15.18it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  81% 1100/1361 [01:12<00:17, 15.26it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  82% 1120/1361 [01:13<00:15, 15.33it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  84% 1140/1361 [01:14<00:14, 15.40it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  85% 1160/1361 [01:14<00:12, 15.47it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  87% 1180/1361 [01:15<00:11, 15.55it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  88% 1200/1361 [01:16<00:10, 15.63it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  90% 1220/1361 [01:17<00:08, 15.71it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  91% 1240/1361 [01:18<00:07, 15.79it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  93% 1260/1361 [01:19<00:06, 15.86it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  94% 1280/1361 [01:20<00:05, 15.94it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  96% 1300/1361 [01:21<00:03, 16.01it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  97% 1320/1361 [01:22<00:02, 16.09it/s, loss=0.359, v_num=1]\n",
            "Epoch 49:  98% 1340/1361 [01:22<00:01, 16.16it/s, loss=0.359, v_num=1]\n",
            "Epoch 49: 100% 1360/1361 [01:23<00:00, 16.23it/s, loss=0.359, v_num=1]\n",
            "Epoch 49: 100% 1360/1361 [01:25<00:00, 16.00it/s, loss=0.343, v_num=1, Eval|mrr=0.205, Eval|hits@1=0.127, Eval|hits@3=0.235, Eval|hits@10=0.353]\n",
            "Epoch 68:  83% 220/265 [00:27<00:05,  7.88it/s, loss=0.299, v_num=1, Eval|mrr=0.205, Eval|hits@1=0.127, Eval|hits@3=0.235, Eval|hits@10=0.353]"
          ]
        }
      ]
    }
  ]
}